package Servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import test.teste;
import service.Service;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "Servlet", value = "/Servlet")
public class Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /*
        * signature	微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
          timestamp	时间戳
          nonce	随机数
          echostr	随机字符串*/
       /* String signature=request.getParameter("signature");
        String timestamp=request.getParameter("timestamp");
        String nonce=request.getParameter("nonce");
        String echostr=request.getParameter("echostr");
        if(Service.check(timestamp,nonce,signature)){
            System.out.println("接收成功");
            PrintWriter out=response.getWriter();
            out.print(echostr);
            out.flush();
            out.close();
        }
        else{
            System.out.println("接入失败");
        }*/

        String json="{\"name\":\"efg\",\"psd\":\"abc\"}";
        //JSONObject json_test = JSONObject.fromObject(json);
        response.setHeader("Access-Control-Allow-Origin", "*");
        /* 星号表示所有的域都可以接受， */
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/javascript;charset=utf-8");


        String name=request.getParameter("name");
       // String age=request.getParameter("age");
        PrintWriter out = response.getWriter();
        //out.write("{\"name\":\"舞蹈家\"}");
        out.write("{\"name\":\"efg\",\"psd\":\"abc\"}");
        out.close();
        System.out.println(name);

    }

    @Override
    //接收消息和事件推送
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      /* System.out.println("KKK");
        ServletInputStream inputStream= request.getInputStream();
        byte[] b=new byte[1024];
        int len;
        StringBuilder sb=new StringBuilder();
        while ((len=inputStream.read(b))!=-1){
            sb.append(new String(b,0,len));
        }
        System.out.println(sb.toString());*/
        //处理消息和事件推送
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("UTF-8");
        Map<String,String> requestMap=Service.parseRequest(request.getInputStream());
        System.out.println(requestMap);
        String respXml=Service.getRespose(requestMap);
       /// String respXml1="<xml><ToUserName><![CDATA["+requestMap.get("FromUserName")+"]]></ToUserName><FromUserName><![CDATA["+requestMap.get("ToUserName")+"]]></FromUserName> <CreateTime>"+System.currentTimeMillis()/1000+"</CreateTime> <MsgType><![CDATA[text]]></MsgType><Content><![CDATA[看看]]></Content></xml>";
       // System.out.println(respXml1);
        System.out.println(respXml);
        PrintWriter out=response.getWriter();
        out.print(respXml);
        out.flush();
        out.close();

    }
}
